package com.zhongjinkang.rhythm.View

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Matrix
import android.graphics.drawable.BitmapDrawable
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import kotlin.math.abs
import kotlin.math.pow
import kotlin.math.sqrt


class MyImageView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : androidx.appcompat.widget.AppCompatImageView(context, attrs) {
    private val matrix = Matrix()

    private val downOne: FloatArray by lazy {
        floatArrayOf(0f, 0f, 0f)
    };
    private val downTwo: FloatArray by lazy {
        floatArrayOf(0f, 0f, 0f)
    };
    var originBitmap: Bitmap? = null





    override fun onTouchEvent(event: MotionEvent?): Boolean {
        event?.let {
            scaleType=ScaleType.MATRIX
            val action = it.actionMasked
            if (action == MotionEvent.ACTION_DOWN) {
                downOne[0] = it.actionIndex.toFloat()
                downOne[1] = it.getX(it.actionIndex)
                downOne[2] = it.getY(it.actionIndex)


            } else if (action == MotionEvent.ACTION_POINTER_DOWN) {
                downTwo[0] = it.actionIndex.toFloat()
                downTwo[1] = it.getX(it.actionIndex)
                downTwo[2] = it.getY(it.actionIndex)


            } else if (action == MotionEvent.ACTION_MOVE) {
                if (it.pointerCount >= 2) {
                    val downDistance =
                        getTwoPointDistance(downOne[1], downOne[2], downTwo[1], downTwo[2])
                    val moveDistance = getTwoPointDistance(
                        it.getX(downOne[0].toInt()),
                        it.getY(downOne[0].toInt()),
                        it.getX(downTwo[0].toInt()),
                        it.getY(downTwo[0].toInt())
                    )

                    if (downDistance > moveDistance) {
                        //缩小
                        smallBitmap(downDistance,moveDistance)


                    } else if (downDistance < moveDistance) {
                        //放大

                        bigBitmap(downDistance,moveDistance,getCenterPoint(downOne[1], downOne[2], downTwo[1], downTwo[2]))

                    } else {

                    }
                } else {
//                    moveToPic(downOne[1], downOne[2], downTwo[1], downTwo[2])
                }


            } else {

            }

        }
        return true
    }

    //计算两点的距离
    private fun getTwoPointDistance(x1: Float, y1: Float, x2: Float, y2: Float): Double {
        val x = (x2 - x1).toDouble().pow(2.0)
        val y = (y2 - y1).toDouble().pow(2.0)
        return sqrt(x + y)
    }

   private fun getCenterPoint(x1: Float, y1: Float, x2: Float, y2: Float): Array<Int> {
       var xCenter = 0
       var yCenter = 0;
       xCenter = if (x1 > x2) {
           ((x1 - x2) / 2 + x2).toInt();
       } else {
           ((x2 - x1) / 2 + x1).toInt();


       }
       yCenter = if (y1 > y2) {
           ((y1 - y2) / 2 + y2).toInt()
       } else {
           ((y2 - y1) / 2 + y1).toInt()
       }
       return arrayOf(xCenter,yCenter)
   }
    //获取到中心点
    private fun moveToPic(x1: Float, y1: Float, x2: Float, y2: Float) {
        var xCenter = 0
        var yCenter = 0;
        xCenter = if (x1 > x2) {
            ((x1 - x2) / 2 + x2).toInt();
        } else {
            ((x2 - x1) / 2 + x1).toInt();


        }
        yCenter = if (y1 > y2) {
            ((y1 - y2) / 2 + y2).toInt()
        } else {
            ((y2 - y1) / 2 + y1).toInt()
        }
        matrix.postTranslate(0f,1000f)
        val tmp = drawable as BitmapDrawable

        if (tmp.bitmap != originBitmap && !tmp.bitmap.isRecycled) {
            tmp.bitmap.recycle()
        }

        // 根据原始位图和 Matrix创建新图片
        val bitmap2: Bitmap = Bitmap.createBitmap(
            originBitmap!!, 0, 0, originBitmap!!.width, originBitmap!!.height,
            matrix, false
        )
        setImageBitmap(bitmap2)
//        matrix.reset()
    }


    private fun smallBitmap(allDistance: Double, currentDistance: Double) {
        val value=currentDistance/allDistance

        matrix.postScale(value.toFloat(), value.toFloat())
        val tmp = drawable as BitmapDrawable

        if (tmp.bitmap != originBitmap && !tmp.bitmap.isRecycled) {
            tmp.bitmap.recycle()
        }

        // 根据原始位图和 Matrix创建新图片
        val bitmap2: Bitmap = Bitmap.createBitmap(
            originBitmap!!, 0, 0, originBitmap!!.width, originBitmap!!.height,
            matrix, false
        )
        matrix.reset()

        //显示新的位图
        //显示新的位图
        setImageBitmap(bitmap2)

    }

    private fun bigBitmap(allDistance: Double, currentDistance: Double,centerPoint:Array<Int>) {
       val  value=currentDistance/allDistance

        matrix.postScale(value.toFloat(), value.toFloat())

        val tmp = drawable as BitmapDrawable

        if (tmp.bitmap != originBitmap && !tmp.bitmap.isRecycled) {
            tmp.bitmap.recycle()
        }

        // 根据原始位图和 Matrix创建新图片
        var startX= abs(width/2f- centerPoint[0]).toInt()

        var startY= abs(height/2f-centerPoint[1]).toInt()
//        matrix.preTranslate(startX.toFloat(),startY.toFloat())
        val bitmap2: Bitmap = Bitmap.createBitmap(
            originBitmap!!,startX,startY, originBitmap!!.width-startX, originBitmap!!.height-startY,
            matrix, false
        )

        matrix.reset()
        //显示新的位图
        //显示新的位图
        setImageBitmap(bitmap2)
    }
}